package com.iot.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.iot.common.annotation.DataScope;
import com.iot.common.utils.DateUtils;
import com.iot.system.domain.DeviceEnergy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.iot.system.mapper.DeviceYieldsMapper;
import com.iot.system.domain.DeviceYields;
import com.iot.system.service.IDeviceYieldsService;
import org.springframework.util.CollectionUtils;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author iot
 * @date 2024-11-18
 */
@Service
@Slf4j
public class DeviceYieldsServiceImpl implements IDeviceYieldsService 
{
    @Autowired
    private DeviceYieldsMapper deviceYieldsMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public DeviceYields selectDeviceYieldsById(Long id)
    {
        return deviceYieldsMapper.selectDeviceYieldsById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param deviceYields 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DeviceYields> selectDeviceYieldsList(DeviceYields deviceYields)
    {
        return deviceYieldsMapper.selectDeviceYieldsList(deviceYields);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param deviceYields 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDeviceYields(DeviceYields deviceYields)
    {
        deviceYields.setCreateTime(DateUtils.getNowDate());
        return deviceYieldsMapper.insertDeviceYields(deviceYields);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param deviceYields 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDeviceYields(DeviceYields deviceYields)
    {
        deviceYields.setUpdateTime(DateUtils.getNowDate());
        return deviceYieldsMapper.updateDeviceYields(deviceYields);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDeviceYieldsByIds(Long[] ids)
    {
        return deviceYieldsMapper.deleteDeviceYieldsByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDeviceYieldsById(Long id)
    {
        return deviceYieldsMapper.deleteDeviceYieldsById(id);
    }

    @Override
    public Integer getDeviceYieldsData(Date nowDate) {
        Integer amountYields = deviceYieldsMapper.getDeviceYieldsData(nowDate);
        if (amountYields != null) {
            return amountYields;
        }
        return 0;
    }

    @Override
    public Integer getLineHourYields(List<Long> collect, Date lastHourStartByDate, Date lastHourEndByDate) {
        log.info("CoreLineHourEnergyInfoServiceImpl_getLineHourYields_获取产线小时产量开始_设备id:{},开始时间:{},结束时间:{}",collect,lastHourStartByDate,lastHourEndByDate);
        //判断当前时间是否计算完成
        List<DeviceYields> deviceYieldsList = deviceYieldsMapper.getLineHourYields(collect,lastHourStartByDate,lastHourEndByDate);
        //判断是否为空
        if (CollectionUtils.isEmpty(deviceYieldsList)){
            return 0;
        }
        List<Integer> energyAccumulate = new ArrayList<>();
        Map<Long, List<DeviceYields>> listMap = deviceYieldsList.stream().collect(Collectors.groupingBy(DeviceYields::getDeviceId));
        listMap.forEach((k,v)->{
            if (Objects.equals(k,6L)){
                int sum = v.stream().map(DeviceYields::getYieldsCounting).mapToInt(Integer::parseInt).sum();
                energyAccumulate.add(sum);
            };
        });
        if (!CollectionUtils.isEmpty(energyAccumulate)){
            Integer sum = energyAccumulate.stream().reduce(0, Integer::sum);
            log.info("CoreLineHourEnergyInfoServiceImpl_getLineHourYields_获取产线小时产量结束_计算结果为:{}",sum);
            return sum;
        }
        return 0;
    }
}
